/*
 * TODO: this should probably use generics (rather than casting) for type safty
 */

package emalaedesktopapplication.forms.admin;

import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class AdminListChangePanel extends javax.swing.JPanel {

    /** Creates new form AdminListChangePanel */
    public AdminListChangePanel() {
        initComponents();
        customInitComponents();
    }

    public AdminListChangePanel(Object[] objects)
    {
        this();
        updateObjectList(objects);
    }

    final public void updateObjectList(Object[] objects)
    {
        this.objectList.setListData(objects);
    }

    public void addButtonsListener(ActionListener al) {
        editObjectButton.addActionListener(al);
        newObjectButton.addActionListener(al);
        deleteObjectButton.addActionListener(al);
    }

    /**
     * Listens to the double click event, refs #29
     */
    public void customInitComponents()
    {
        MouseListener keyListener = new MouseListener()
        {
            public void mouseClicked(MouseEvent me)
            {
                if (me.getClickCount() == 2)
                {
                    int index = objectList.locationToIndex(me.getPoint());
                    // simulates edit button click
                    editObjectButton.doClick();
                }
            }

            public void mousePressed(MouseEvent me)
            {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent me)
            {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent me)
            {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent me)
            {
                // throw new UnsupportedOperationException("Not supported yet.");
            }
        };

        objectList.addMouseListener(keyListener);
    }

    public Object getSelectedItem()
    {
       return objectList.getSelectedValue();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        objectList = new javax.swing.JList();
        editObjectButton = new javax.swing.JButton();
        newObjectButton = new javax.swing.JButton();
        deleteObjectButton = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(emalaedesktopapplication.EmaLaeDesktopApplication.class).getContext().getResourceMap(AdminListChangePanel.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        objectList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        objectList.setName("objectList"); // NOI18N
        jScrollPane1.setViewportView(objectList);

        editObjectButton.setText(resourceMap.getString("editObjectButton.text")); // NOI18N
        editObjectButton.setName("editObjectButton"); // NOI18N

        newObjectButton.setText(resourceMap.getString("newObjectButton.text")); // NOI18N
        newObjectButton.setName("newObjectButton"); // NOI18N

        deleteObjectButton.setText(resourceMap.getString("deleteObjectButton.text")); // NOI18N
        deleteObjectButton.setName("deleteObjectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(editObjectButton)
                        .addGap(18, 18, 18)
                        .addComponent(newObjectButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteObjectButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editObjectButton)
                    .addComponent(newObjectButton)
                    .addComponent(deleteObjectButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteObjectButton;
    private javax.swing.JButton editObjectButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newObjectButton;
    private javax.swing.JList objectList;
    // End of variables declaration//GEN-END:variables

}
